package org.example.controller;

import dto.ExceptionTrackingQueryDTO;
import dto.InspectionQualityQueryDTO;
import dto.StandardQuaryDTO;
import entity.ExceptionTracking;
import entity.MaterialOrder;
import entity.QualityInspection;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.constant.MessageConstant;
import org.example.result.PageResult;
import org.example.result.Result;
import org.example.service.QualityManagerService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@Tag(name = "质量经理控制层")
@RequestMapping("/quality-manager")
@RestController
@RequiredArgsConstructor
public class QualityManagerController {

    private final QualityManagerService qualityManagerService;

    /**
     * 查询质检单
     */
    @Operation(description = "查询质检单")
    @GetMapping("/queryQualityInspection")
    public Result<PageResult> queryQualityInspection(
            @RequestParam(name = "pageNumber") Integer pageNumber,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "inspectType", required = false) String inspectType,
            @RequestParam(name = "inspectStatus", required = false) String inspectStatus,
            @RequestParam(name = "inspectCategory", required = false) String inspectCategory,
            @RequestParam(name = "inspectionId", required = false) Integer inspectionId) {
        log.info("查询质检单：page = {}, pageSize = {}, inspectType = {} ,inspectStatus = {}, inspectCategory = {}, inspectionId = {}", pageNumber, pageSize, inspectType,
                inspectStatus, inspectCategory, inspectionId);
        InspectionQualityQueryDTO queryDTO = InspectionQualityQueryDTO.builder()
                .pageNumber(pageNumber)
                .pageSize(pageSize)
                .inspectStatus(inspectStatus)
                .inspectCategory(inspectCategory)
                .inspectType(inspectType)
                .inspectionId(inspectionId)
                .build();
        PageResult pageResult = qualityManagerService.queryQualityInspection(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 审核质检单
     */
    @Operation(description = "审核质检单")
    @PutMapping("/reviewQualityInspection")
    public Result<String> reviewQualityInspection(@RequestBody QualityInspection qualityInspection) {
        log.info("审核质检单：qualityInspection = {}", qualityInspection);
        qualityManagerService.reviewQualityInspection(qualityInspection);
        return Result.success(MessageConstant.UPLOAD_SUCCESS);
    }

    /**
     * 查询质量标准
     */
    @Operation(description = "查询质量标准")
    @GetMapping("/queryQualityStandard")
    public Result<PageResult> queryQualityStandard(
            @RequestParam(name = "pageNum") Integer pageNum,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "standardId", required = false) Integer standardId,
            @RequestParam(name = "standardType", required = false) String standardType) {
        log.info("查询质量标准：pageNum = {}, pageSize = {}, standardId = {}, standardType = {}", pageNum, pageSize, standardId, standardType);
        StandardQuaryDTO queryDTO = StandardQuaryDTO.builder()
                .pageNum(pageNum)
                .pageSize(pageSize)
                .standardId(String.valueOf(standardId))
                .StandardType(standardType)
                .build();
        PageResult pageResult = qualityManagerService.queryQualityStandard(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 获取质量数据看板
     */
    @Operation(description = "获取质量数据看板")
    @GetMapping("/getQualityDashboard")
    public Result<Map<String, Object>> getQualityDashboard(
            @RequestParam(name = "dateRange", required = false) List<String> dateRange,
            @RequestParam(name = "inspectionType", required = false) String inspectionType,
            @RequestParam(name = "supplier", required = false) String supplier,
            @RequestParam(name = "defectType", required = false) String defectType) {
        log.info("获取质量数据看板: dateRange={}, inspectionType={}, supplier={}, defectType={}",
                dateRange, inspectionType, supplier, defectType);
        Map<String, Object> dashboard = qualityManagerService.getQualityDashboard(dateRange, inspectionType, supplier, defectType);
        return Result.success(dashboard);
    }


    /**
     * 分页查询异常追踪列表
     */
    @GetMapping("/exception-list")
    @Operation(description = "分页查询异常追踪列表")
    public Result<PageResult> getExceptionList(
            @RequestParam(name = "pageNum") Integer pageNum,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "processType", required = false) String processType,
            @RequestParam(name = "currentStatus", required = false) String currentStatus,
            @RequestParam(name = "dateRange", required = false) List<String> dateRange,
            @RequestParam(name = "keyword", required = false) String keyword
    ) {
        ExceptionTrackingQueryDTO queryDTO = ExceptionTrackingQueryDTO.builder()
                .pageNum(pageNum)
                .pageSize(pageSize)
                .processType(processType)
                .currentStatus(currentStatus)
                .dateRange(dateRange)
                .keyword(keyword)
                .build();
        PageResult pageResult = qualityManagerService.getExceptionList(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 异常追踪详情
     */
    @GetMapping("/exception-tracking/{exceptionNumber}")
    @Operation(description = "异常追踪详情")
    public Result<List<MaterialOrder>> getExceptionTracking(@PathVariable String exceptionNumber) {
        log.info("异常追踪详情：exceptionNumber = {}", exceptionNumber);
        List<MaterialOrder> materialOrder = qualityManagerService.getTracking(exceptionNumber);
        return Result.success(materialOrder);
    }

    @PostMapping("/addExceptionTracking")
    @Operation(description = "新增异常追踪")
    public Result addExceptionTracking(@RequestBody ExceptionTracking exceptionTracking) {
        qualityManagerService.addExceptionTracking(exceptionTracking);
        return Result.success();
    }

    //更新信息，修改异常追踪
    @PutMapping("/updateExceptionTracking")
    @Operation(description = "更新异常追踪")
    public Result updateExceptionTracking(@RequestBody ExceptionTracking exceptionTracking) {
        qualityManagerService.updateExceptionTracking(exceptionTracking);
        return Result.success();
    }

    /**
     * 异常详情
     */
    @GetMapping("/exception-detail/{exceptionNumber}")
    @Operation(description = "异常详情")
    public Result<ExceptionTracking> getExceptionDetail(@PathVariable String exceptionNumber) {
        ExceptionTracking exceptionTracking = qualityManagerService.getExceptionDetail(exceptionNumber);
        return Result.success(exceptionTracking);
    }

    /**
     * 折线图呈现各异常每月出现数量
     */
    @GetMapping("/getExceptionTracking")
    @Operation(description = "折线图呈现各异常每月出现数量")
    public Result<Map<String, Object>> getLineChatException(
    ){
        log.info("调取折线图呈现各异常每月出现数量");
        Map<String, Object> LineChatException = qualityManagerService.getLineChatException();
        return Result.success(LineChatException);
    }

    /**
     * 导出数据
     * @param response
     */
    @GetMapping("/export/inspection")
    @Operation(summary = "导出数据")
    public void exportInspection(@RequestParam List<Integer> inspectionIds, HttpServletResponse response){
        log.info("导出数据");
        qualityManagerService.exportInspection(inspectionIds,response);
    }

    /**
     * 列出不合格质检列表
     */
    @GetMapping("/list/inspection")
    @Operation(summary = "列出不合格质检列表")
    public Result<List<Integer>> listUnqualifiedInspection(){
        log.info("列出不合格质检列表");
        return Result.success(qualityManagerService.listUnqualifiedInspection());
    }

    /**
     * 获取异常跟踪时间线
     */
    @GetMapping("/tracking-timeline/{exceptionNumber}")
    @Operation(summary = "获取异常跟踪时间线")
    public Result<List<Map<String, Object>>> getTrackingTimeline(@PathVariable String exceptionNumber) {
        log.info("获取异常跟踪时间线：exceptionNumber = {}", exceptionNumber);
        List<Map<String, Object>> timeline = qualityManagerService.getTrackingTimeline(exceptionNumber);
        return Result.success(timeline);
    }
}